<?php
Class Controller_Product_Image extends Controller_Abstract
{
	function actionCreate()
	{
		$this->_view['title'] = '需求单创建(基本资料)';
		$this->_view['user'] = User::getOnDutyUser();
		if (isset($_POST['ids']))
		{
			$ids = json_decode($_POST['ids']);
			$product = Product_Basic::find('id in ( ' . implode(',', $ids) . ')')->asArray()->getAll();
			$this->_view['product'] = $product;
		}
		else
		{
			return $this->_redirectMessage('没有选中产品', '正在返回基本资料管理列表', url('Product::BasicInfo/List'), FAILURE);
		}
	}
	
	function actionWareHouseCreate()
	{
		$this->_view['title'] = '需求单创建(库存资料)';
			$this->_view['user'] = User::getOnDutyUser();
		if (isset($_POST['ids']))
		{
			$ids = json_decode($_POST['ids']);
			$product = Product_Basic::find('id in ( ' . implode(',', $ids) . ')')->asArray()->getAll();
			$this->_view['product'] = $product;
		}
		else
		{
			return $this->_redirectMessage('没有选中产品', '正在返回库存资料管理列表', url('Product::WareHouse/List'), FAILURE);
		}
	}
	
	function actionCreateSave()
	{
		$this->_view['title'] = '需求单创建（保存）';
		if ($this->_context->isPOST())
		{
			$group = array();
			foreach ($_POST as $key => $value)
			{
				$temp_arr = explode('_', $key);
				if ($temp_arr[0] == 'group')
				{
					$group_id = $temp_arr[1];
					$group[$group_id][$temp_arr[2]] = $value;
				}
				if ($temp_arr[0] == 'product')
				{
					$group[$group_id]['item'][][$temp_arr[1]] = $value;
				}
			}
			$create = array(
				'name' => trim($_POST['name']),
				'requirement_user_id' => intval($_POST['requirement_user_id']),
				'type' => intval($_POST['type']),
				'note' => trim($_POST['note']),
				'group' => $group
			);
			$result = Image_Requirement::create($create);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('保存成功', '返回拍摄列表', url('Product::Image/PhotoList'), $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('保存失败', '返回拍摄列表', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
		else
		{
			return $this->_redirectMessage('保存失败', '不是提交动作，返回拍摄列表', url('Product::Image/PhotoList'), $result['ack']);
		}
	}
	
	function actionProductInfoAjax()
	{
		$this->_view['title'] = '产品信息请求ajax';
		$product_basic = Product_Basic::search(array('number_or_name' => $_REQUEST['q']), 0, 10);
		$result = array();
		foreach ($product_basic['data'] as $pb)
		{
			$result[] = array(
				0 => $pb['number'] . ' ' . $pb['name'],
				'id' => $pb['id'],
				'number' => $pb['number']
			);
		}
		echo json_encode($result);
		exit;
	}
	
	function actionEdit()
	{
		$this->_view['title'] = '需求单编辑';
		if ($_GET['id'])
		{
			$requirement = Image_Requirement::find('id=?', $_GET['id'])->asArray()->getOne();
			if ($requirement)
			{
				$this->_view['user'] = User::getOnDutyUser();
				$group = Image_Requirement_Group::find('image_requirement_id=?', $requirement['id'])->asArray()->getAll();
				foreach ($group as &$g)
				{
					$item = Image_Requirement_Group_Item::find('image_requirement_group_item.image_requirement_group_id=?', $g['id'])
						->joinLeft('product_basic', '', 'product_basic.id=image_requirement_group_item.product_basic_id')
						->setColumns('image_requirement_group_item.*, product_basic.number as product_number, product_basic.name as product_name')
						->asArray()->getAll();
					$g['item'] = $item;
				}
				unset($g);
				$requirement['group'] = $group;
				$this->_view['requirement'] = $requirement;
			}
			else
			{
				return $this->_redirectMessage('编辑失败', '找不到需求单', url('Product::Image/PhotoList'),  FAILURE);
			}
		}
	}
	
	function actionEditSave()
	{
		$this->_view['title'] = '需求单编辑（保存）';
		if ($this->_context->isPOST())
		{
			$group = array();
			foreach ($_POST as $key => $value)
			{
				$temp_arr = explode('_', $key);
				if ($temp_arr[0] == 'group')
				{
					$group_id = $temp_arr[1];
					$group[$group_id][$temp_arr[2]] = $value;
				}
				if ($temp_arr[0] == 'product')
				{
					$group[$group_id]['item'][$temp_arr[2]][$temp_arr[1]] = $value;
				}
			}
			$update = array(
				'id' => intval($_POST['requirement_id']),
				'requirement_user_id' => intval($_POST['requirement_user_id']),
				'name' => trim($_POST['name']),
				'type' => intval($_POST['type']),
				'note' => trim($_POST['note']),
				'group' => $group
			);
			$result = Image_Requirement::updateRequirement($update);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('保存成功', '返回拍摄列表', url('Product::Image/PhotoList'), $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('保存失败', $resule['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
		else
		{
			return $this->_redirectMessage('保存失败', '不是提交动作，返回拍摄列表', url('Product::Image/PhotoList'), FAILURE);
		}
	}
	
	function actionDelete()
	{
		$this->_view['title'] = '需求单删除';
		if (isset($_GET['id']) && $_GET['id'])
		{
			$result = Image_Requirement::delete($_GET['id']);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('删除成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('删除失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
		else
		{
			return $this->_redirectMessage('删除失败', '没有选中需求单', url('Product::Image/PhotoList'), FAILURE);
		}
	}
	
	function actionPhotoList()
	{
		$this->_view['title'] = '拍摄列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$user = Purchase_Order::getUser(true);
		$this->_view['user'] = $user['data'];
		$condition = Helper_BSS_Normal::buildCondition();
		$condition['photo_handle_status'] = 1;
		$result = Image_Requirement::search($condition, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$requirement = $result['data'];
			foreach ($requirement as &$r)
			{
				$group = Image_Requirement_Group::find('image_requirement_id=?', $r['id'])->asArray()->getAll();
				foreach ($group as &$g)
				{
					$item = Image_Requirement_Group_Item::find('image_requirement_group_item.image_requirement_group_id=?', $g['id'])
						->joinLeft('product_basic', '', 'product_basic.id=image_requirement_group_item.product_basic_id')
						->setColumns('image_requirement_group_item.*, product_basic.number as product_number, product_basic.name as product_name')
						->asArray()->getAll();
					$g['item'] = $item;
				}
				unset($g);
				$r['group'] = $group;
			}
			unset($r);
			$page = new Helper_BSS_Page($result['pagination'], url('Product::Image/PhotoList', $condition));
			$this->_view['page'] = $page->getPage();
			$this->_view['requirement'] = $requirement;
		}
	}
	
	function actionShowStatus()
	{
		$this->_view['title'] = '显示状态';
		if (isset($_GET['id']))
		{
			$result = Image_Requirement::search(array('id' => $_GET['id']), 0, 0);
			$this->_view['requirement'] = $result['data'][0];
		}
	}
	
	function actionRequirementNote()
	{
		$this->_view['title'] = '备注';
		$requirement = Image_Requirement::find('id=?', $_GET['id'])->asArray()->getOne();
		$this->_view['note'] = $requirement['note'];
	}
	
	function actionRequirementConfirmed()
	{
		$this->_view['title'] = '需求确认';
		if ( ! empty($_POST['id']))
		{
			if (empty($_POST['is_photo_confirmed']))
			{
				$result = Image_Requirement::changeHandleStatusfromNewToRequirementConfirmedOrPhotoConfirmed($_POST['id']);
			}
			else
			{
				if ( ! empty($_POST['photo_path']))
				{
					$result = Image_Requirement::changeHandleStatusfromNewToRequirementConfirmedOrPhotoConfirmed($_POST['id'], $_POST['photo_path']);
				}
				else
				{
					return $this->_redirectMessage('需求确认失败', '拍摄图片路径不能为空', $_SERVER['HTTP_REFERER'], FAILURE);
				}
			}
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('需求确认成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('需求确认失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionPhotoAccepted()
	{
		$this->_view['title'] = '拍摄接受';
		if ( ! empty($_GET['id']))
		{
			$result = Image_Requirement::changeHandleStatusFromRequirementConfirmedToPhotoAccepted($_GET['id']);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('拍摄接受成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('拍摄接受失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionPhotoCompleted()
	{
		$this->_view['title'] = '拍摄完成';
		if ( ! empty($_POST['id']))
		{
			if ( ! empty($_POST['photo_path']))
			{
				$result = Image_Requirement::changeHandleStatusFromPhotoAcceptedToPhotoCompleted($_POST['id'], $_POST['photo_path']);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('拍摄完成成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('拍摄完成失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('拍摄完成失败', '拍摄图片路径不能为空！', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionPhotoPathSave()
	{
		$this->_view['title'] = '拍摄路径（保存）';
		if ( ! empty($_POST['id']))
		{
			if ( ! empty($_POST['photo_path']))
			{
				$result = Image_Requirement::updatePhotoPath($_POST['id'], $_POST['photo_path']);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('保存成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('保存失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('保存拍摄路径失败', '拍摄路径不能为空！', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionRephoto()
	{
		$this->_view['title'] = '返拍';
		if ( ! empty($_POST['id']))
		{
			if ( ! empty($_POST['rephoto_reason']))
			{
				$result = Image_Requirement::changeHandleStatusFromPhotoCompletedOrRephotoCompletedToRephoto($_POST['id'], $_POST['rephoto_reason']);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('返拍成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('返拍失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}				
			}
			else
			{
				return $this->_redirectMessage('返拍失败', '返拍原因不能为空！', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionRephotoReasonSave()
	{
		$this->_view['title'] = '返拍原因（保存）';
		if ( ! empty($_POST['id']))
		{
			if ( ! empty($_POST['rephoto_reason']))
			{
				$result = Image_Requirement::UpdateRephotoReason($_POST['id'], $_POST['rephoto_reason']);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('保存成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('保存失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('保存失败', '返拍原因不能为空！', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionRephotoAccepted()
	{
		$this->_view['title'] = '返拍接受';
		if ( ! empty($_GET['id']))
		{
			$result = Image_Requirement::changeHandleStatusFromRephotoToRephotoAccepted($_GET['id']);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('返拍接受成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('返拍接受失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionRephotoCompleted()
	{
		$this->_view['title'] = '返拍完成';
		if ( ! empty($_POST['id']))
		{
			if ( ! empty($_POST['photo_path']))
			{
				$result = Image_Requirement::changeHandleStatusFromRephotoAcceptedToRephotoCompleted($_POST['id'], $_POST['photo_path']);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('返拍完成成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('返拍完成失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('返拍完成失败', '拍摄图片路径不能为空！', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionPhotoConfirmed()
	{
		$this->_view['title'] = '拍摄确认';
		if ( ! empty($_GET['id']))
		{
			$result = Image_Requirement::changeHandleStatusFromPhotoCompletedOrRephotoCompletedToPhotoConfirmed($_GET['id']);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('拍摄确认成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('拍摄确认失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionDesignList()
	{
		$this->_view['title'] = '设计列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$user = Purchase_Order::getUser();
		$condition = Helper_BSS_Normal::buildCondition();
		$condition['design_handle_status'] = 1;
		$result = Image_Requirement::search($condition, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$requirement = $result['data'];
			foreach ($requirement as &$r)
			{
				$group = Image_Requirement_Group::find('image_requirement_id=?', $r['id'])->asArray()->getAll();
				foreach ($group as &$g)
				{
					$item = Image_Requirement_Group_Item::find('image_requirement_group_item.image_requirement_group_id=?', $g['id'])
						->joinLeft('product_basic', '', 'product_basic.id=image_requirement_group_item.product_basic_id')
						->setColumns('image_requirement_group_item.*, product_basic.number as product_number, product_basic.name as product_name')
						->asArray()->getAll();
					$g['item'] = $item;
				}
				unset($g);
				$r['group'] = $group;
			}
			unset($r);
			$page = new Helper_BSS_Page($result['pagination'], url('Product::Image/DesignList', $condition));
			$this->_view['user'] = $user['data'];
			$this->_view['page'] = $page->getPage();
			$this->_view['requirement'] = $requirement;
			$where = Image_Requirement::buildSearchWhere($condition);
			$photo_completed_report = QDB::getConn()->execute('select user.name,count(*) as amount,sum(image_requirement.product_quantity) as product_quantity_amount from image_requirement left join user on image_requirement.photo_completed_user_id=user.id where ' . $where . ' and image_requirement.photo_completed_user_id>0 group by user.id')->fetchAll();
			$design_completed_report = QDB::getConn()->execute('select user.name,count(*) as amount,sum(image_requirement.product_quantity) as product_quantity_amount from image_requirement left join user on image_requirement.design_completed_user_id=user.id where ' . $where . ' and image_requirement.design_completed_user_id>0 group by user.id')->fetchAll();
			$this->_view['photo_completed_report'] = $photo_completed_report;
			$this->_view['design_completed_report'] = $design_completed_report;
		}
	}
	
	function actionDesignAccepted()
	{
		$this->_view['title'] = '设计接受';
		if ( ! empty($_GET['id']))
		{
			$result = Image_Requirement::changeHandleStatusFromPhotoConfirmedToDesignAccepted($_GET['id']);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('设计接受成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('设计接受失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionDesignCompleted()
	{
		$this->_view['title'] = '设计完成';
		if ( ! empty($_POST['id']))
		{
			if ( ! empty($_POST['design_path']))
			{
				$result = Image_Requirement::changeHandleStatusFromDesignAcceptedToDesignCompleted($_POST['id'], $_POST['design_path']);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('设计完成成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('设计完成失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('设计完成失败', '设计图片路径不能为空！', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionDesignPathSave()
	{
		$this->_view['title'] = '设计路径（保存）';
		if ( ! empty($_POST['id']))
		{
			if ( ! empty($_POST['design_path']))
			{
				$result = Image_Requirement::updateDesignPath($_POST['id'], $_POST['design_path']);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('保存成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('保存失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('保存失败', '设计路径不能为空！', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionRedesign()
	{
		$this->_view['title'] = '返修';
		if ( ! empty($_POST['id']))
		{
			if ( ! empty($_POST['redesign_reason']))
			{
				$result = Image_Requirement::changeHandleStatusFromDesignCompletedOrRedesignCompletedToRedesign($_POST['id'], $_POST['redesign_reason']);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('返修成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('返修失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('返修失败', '返修原因不能为空！', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionRedesignReasonSave()
	{
		$this->_view['title'] = '返修原因（保存）';
		if ( ! empty($_POST['id']))
		{
			if ( ! empty($_POST['redesign_reason']))
			{
				$result = Image_Requirement::UpdateRedesignReason($_POST['id'], $_POST['redesign_reason']);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('保存成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('保存失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('保存失败', '返拍原因不能为空！', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionRedesignAccepted()
	{
		$this->_view['title'] = '返修接受';
		if ( ! empty($_GET['id']))
		{
			$result = Image_Requirement::changeHandleStatusFromRedesignToRedesignAccepted($_GET['id']);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('返修接受成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('返修接受失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
	
	function actionReDesignCompleted()
	{
		$this->_view['title'] = '返修完成';
		if ( ! empty($_POST['id']))
		{
			if ( ! empty($_POST['design_path']))
			{
				$result = Image_Requirement::changeHandleStatusFromRedesignAcceptedToRedesignCompleted($_POST['id'], $_POST['design_path']);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('返修完成成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('返修完成失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('返修完成失败', '设计图片路径不能为空！', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionDesignConfirmed()
	{
		$this->_view['title'] = '设计确认';
		if ( ! empty($_GET['id']))
		{
			$result = Image_Requirement::changeHandleStatusFromDesignCompletedOrRedesignCompletedToDesignConfirmed($_GET['id']);
			if ($result['ack'] == SUCCESS)
			{
				return $this->_redirectMessage('设计确认成功', '正在返回..', $_SERVER['HTTP_REFERER'], $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('设计确认失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
			}
		}
	}
}